<?php

namespace App\Http\Controllers\Admin\Examine;

use App\Http\Controllers\Controller;
use App\Models\examine\ConfigModel;
use App\Models\examine\RecordCopyModel;
use App\Models\examine\RecordDetailModel;
use App\Models\examine\RecordModel;
use App\Models\examine\ReimburseModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class Copy extends Controller
{

    /**
     * Notes:列表
     * User: xiangzi
     * Date: 2024/9/5
     * Time: 14:35
     * @return \Illuminate\Http\JsonResponse
     */
    public function list(Request $request) {
        $auth = $request->get('auth');
        $page = $request->input('page', 1);
        $limit = $request->input('limit', 20);
        $examine_no = $request->input('examine_no', '');
        $status = $request->input('status', '');
        $type = $request->input('type', '');
        $is_read = $request->input('is_read', '');
        $create_start = $request->input('create_start', '');
        $create_end = $request->input('create_end', '');

        $record_where = [];
        if ($examine_no) {
            $record_where['examine_no'] = $examine_no;
        }
        if($status){
            $record_where['status'] = $status;
        }
        if($type){
            $record_where['type'] = $type;
        }

        $query = RecordCopyModel::query()->with('create_admin')->withWhereHas('record', function ($query) use ($record_where) {
            if ($record_where) {
                $query->where($record_where);
            }
        })->where('admin_id', $auth->id);

        if($is_read){
            $query = $query->where('status', $is_read);
        }

        if($create_start && $create_end){
            $query = $query->whereBetween('created_at', [$create_start, $create_end]);
        }

        $total = $query->count('id');
        $items = $query
            ->offset(($page-1) * $limit)
            ->limit($limit)
            ->orderBy('id', 'desc')
            ->get();


        $list = $items->map(function($item) {
            $res = [
                'id' => $item->id,
                'examine_no' => $item->record->examine_no ?? '',
                'type' => $item->type ?? '',
                'type_name' => ConfigModel::TYPE[$item->type] ?? '',
                'status' => $item->status,
                'status_text' => RecordDetailModel::STATUS[$item->status] ?? '',
                'submit_time' => $item->created_at->toDateTimeString(),
                'submit_user' => $item->create_admin ? $item->create_admin->staff->name: '',
                'is_read' => RecordCopyModel::STATUS[$item->status] ?? '',
            ];
            return $res;
        });

        return $this->success(['list' => $list, 'total' => $total]);
    }


    public function detail($id){
        $model = RecordCopyModel::find($id);
        if (!$model) {
            return $this->error('数据不存在');
        }

        $record = RecordModel::find($model->record_id);

        if (!$record) {
            return $this->error('数据不存在');
        }

        if($model->status == 0){
            $model->status = 1;
            $model->save();
        }

        $return = [
            'base_info' => [
                'examine_type' => ConfigModel::EXAMINE_TYPE[$record->examine_type],
                'type_name' => ConfigModel::TYPE[$record->type] ?? '',
                'submit_user' => $model->create_admin ? $model->create_admin->staff->name : '',
                'submit_time' => $model->created_at->toDateTimeString(),
                'examine_no' => $record->examine_no,
                'examine_status' => RecordModel::STATUS[$record->status],
                'cancel_time' => $record->cancel_time,
                'reason' => $record->reason
            ],
            'examine_info' => [
//                'amount' => $model->amount,
//                'use_to' => $model->use_to,
//                'file' => $model->file,
//                'remarks' => $model->remarks,
            ],
            'examine_process' => (new Record())->examine_info($model->record_id),
            'copy_info' => (new Record())->copy_info($model->record_id)
        ];

        return $this->success($return);

    }

}
